package gitobject;
import repository.Repository;
import zlib.ZLibUtils;
import org.apache.axis.encoding.Base64;

import java.io.*;


public class GitObject {

    protected String fmt;                  //type of object
    protected String key;                  //key of object
    protected String mode;                 //mode of object
    protected static String path = Repository.getGitDir() + File.separator + "objects";          //absolute path of objects
    protected String value;                //value of object

    public String getFmt(){
        return fmt;
    }
    public String getKey() { return key; }
    public String getMode(){
        return mode;
    }
    public String getPath() {
        return path;
    }
    public String getValue(){
        return value;
    }


    public GitObject(){}
    /**
     * Get the value(content) of file
     * @param file
     * @return String
     * @throws IOException
     */
    public static String getValue(File file) throws IOException {
        String filecontents = null;
        try {
            long lenofile = file.length();
            byte[] buffer = new byte[1024];
            FileInputStream fIS = new FileInputStream(file);
            byte[] fileContent = new byte[(int) lenofile];
            fIS.read(fileContent);
            fIS.close();
            filecontents = new String(fileContent, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filecontents;
    }
    /**
     * Todo: Serialize the object to the local repository.
     * @throws Exception
     */
    public void writeObject() throws Exception{
        File fileDir = new File(path + File.separator + key.substring(0, 2));
        fileDir.mkdir();
        File file_2 = new File(path + File.separator + key.substring(0, 2) + File.separator + key.substring(2));
        FileOutputStream fos = new FileOutputStream(file_2);
        ObjectOutputStream file = new ObjectOutputStream(fos);
        file.writeObject(this);
        System.out.println("writeObject successfully executed");
        file.close();
    }

    /**
     * Todo: Serialize the object and compress with zlib.
     * @throws Exception
     */
    public void compressWrite() throws Exception{
        File gitfile = new File(path + File.separator + key.substring(0, 2));
        if (gitfile.exists()) {
            return;
        }
        FileOutputStream fos = new FileOutputStream(gitfile);

        ByteArrayOutputStream byteaos = new ByteArrayOutputStream();
        ObjectOutputStream objos = new ObjectOutputStream(byteaos);
        objos.writeObject(this);
        byte[] bytes = byteaos.toByteArray();

        ZLibUtils.compress(bytes, fos);
        fos.close();
        byteaos.close();
        objos.close();
    }
    /**
     * Todo: Deserialize the object and decompress with zlib.
     * @throws Exception
     */
    public static GitObject deserialize(String Id) throws IOException {
        GitObject gitObj = null;
        try {
            File file = new File(path + File.separator + Id);

            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
                byte[] bytes = ZLibUtils.decompress(fis.readAllBytes());
                fis.close();

                ByteArrayInputStream byteais = new ByteArrayInputStream(bytes);
                ObjectInputStream objis = new ObjectInputStream(byteais);
                gitObj = (GitObject) objis.readObject();
                byteais.close();
                objis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gitObj;
    }
}
